package com.dlss.scjp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

/** Called when the activity is first created. */
public class SCJPv1Activity extends Activity implements Button.OnClickListener{	
    
	private final static int NO_OF_QUESTIONS = 10;
	
	private Button b1;
    private RadioButton c1, c2, c3, c4, c5,c6;
    private TextView et1;
    private Button startButton;       
    private TextView questionText;
    
    private FileReader fileReader = new FileReader();
    private List<Question> questions = null;
    
    private Question currentQuestion;
    
    private static int questionNumber = 0;
    
    private static int questionsCorrect = 0;
    
    private boolean answerTextSet = false;
    
    private Map<Integer,Integer> questionsAsked = new HashMap<Integer,Integer>();
    
    /**
     * When the options menu gets created, this gets called.
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	
    	inflater.inflate(R.menu.options_menu, menu);
    	return true;
    }
    
    /**
     * When the options menu gets selected, this gets called.
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	return OptionsMenu.onOptionsItemSelected(item, this);
    }   
    
    /**
     * When the app starts, this gets called.
     */
    @Override
    public void onCreate(Bundle icicle)
    {
    	super.onCreate(icicle);
    	setContentView(R.layout.frontscreen); 
    	startButton = (Button) findViewById(R.id.frontScreenstartbutton);
    	startButton.setOnClickListener(startButtonOnClickListener);
    }
        
    
    public void showResultsPage()    
    {    
    	setContentView(R.layout.resultscreen);    	
    	et1 = (TextView) this.findViewById(R.id.text2);
	 	et1.setText("\n\nYou got " + questionsCorrect + " questions correct out of " + NO_OF_QUESTIONS);	
	 	questionsCorrect = 0;
	 	questionNumber = 0;
	 	startButton = (Button) findViewById(R.id.startbutton);
   	    startButton.setOnClickListener(startButtonOnClickListener);  
   	    
   	    TextView url = (TextView) this.findViewById(R.id.text7);
   	    
	   	url.setText(
	   			Html.fromHtml("<a href=\"http://scjponandroid.blogspot.com/\">http://scjponandroid.blogspot.com/</a>"));

   	      	   
	   	//url.setText(
	   	//		Html.fromHtml("<a href=\"http://scjponandroid.blogspot.com/\">http://scjponandroid.blogspot.com/</a>"));
    }
    
    View.OnClickListener startButtonOnClickListener = new View.OnClickListener() 
    {
        public void onClick(View v) 
        {                
        	setContentView(R.layout.main);                	
        	go();
        }
    };
      
    
    public void go()
    {    	
    	setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	
    	
    	
    	// load questions from text file
	    questions = fileReader.readFile(this.getResources().openRawResource(R.raw.declerations));
	    Log.i("Go()", "Got " + questions.size());
	    currentQuestion = getRandomQuestion();
	    displayQuestion();   	
    }
    
   
    
    private void displayQuestion()
    {
    	if (questionNumber < NO_OF_QUESTIONS)
    	{    	    		
	    	questionText = (TextView) this.findViewById(R.id.text1);
	    	
	    	questionText.setText("");
	
	    	loadAllOptions();	 	    
	 		 	    
	 	    b1 = (Button) findViewById(R.id.button1);
	 	    b1.setOnClickListener(this);
	 	    
	 	    et1 = (TextView) this.findViewById(R.id.text2);
	 	   
	    	
	    	for (String questionLine : currentQuestion.getLinesOfQuestion())
	    	{
	    		questionText.append(questionLine+"\n");
	    	}
	    	
	    	setOptionText();
	    	//showAllOptions();
	    	

	    	// there will always be at least 2 options displayed
	    	if (currentQuestion.getNoOfOptions() ==2)
	    	{
	    		c1.setVisibility(View.VISIBLE);
	    		c2.setVisibility(View.VISIBLE);
	    		c3.setVisibility(View.GONE);
	    		c4.setVisibility(View.GONE);
	    		c5.setVisibility(View.GONE);
	    	}	    	
	    	else if (currentQuestion.getNoOfOptions() ==3)
	    	{
	    		c1.setVisibility(View.VISIBLE);
	    		c2.setVisibility(View.VISIBLE);
	    		c3.setVisibility(View.VISIBLE);
	    		c4.setVisibility(View.GONE);
	    		c5.setVisibility(View.GONE);	    		
	    	}	    	
	    	else if (currentQuestion.getNoOfOptions() ==4)
	    	{
	    		c1.setVisibility(View.VISIBLE);
	    		c2.setVisibility(View.VISIBLE);
	    		c3.setVisibility(View.VISIBLE);
	    		c4.setVisibility(View.VISIBLE);
	    		c5.setVisibility(View.GONE);	    		
	    	}	
	    	else if (currentQuestion.getNoOfOptions() ==5)
	    	{
	    		c1.setVisibility(View.VISIBLE);
	        	c2.setVisibility(View.VISIBLE);
	        	c3.setVisibility(View.VISIBLE);
	        	c4.setVisibility(View.VISIBLE);
	        	c5.setVisibility(View.VISIBLE);	    		
	    	}	
	    	
	    	uncheckAllOptions();
	    	
	    		    	
    	}
    	else
    	{
    		showResultsPage();
    	}	   
    }
    
    
    
    
    private Question getRandomQuestion()
    {
    	int randomQuestionNumber = (int)(Math.random() * questions.size());
    	
    	Log.i("getRandomQuestion", "randomQuestionNumber is " + randomQuestionNumber);
    	Log.i("getRandomQuestion", "questionsAsked is " + questionsAsked.size());
    	
    	if (questionsAsked.size() >= questions.size()-1)
    	{
    		questionsAsked.clear();
    	}
    	
    	while (true)
    	{
    		if (!questionsAsked.containsKey(randomQuestionNumber))
    		{
    			questionsAsked.put(randomQuestionNumber,randomQuestionNumber);
    			break;
    		}
    		else
    		{
    			Log.i("getRandomQuestion", "random question already asked, getting another");
    			randomQuestionNumber = (int)(Math.random() * questions.size());
    			Log.i("getRandomQuestion", "randomQuestionNumber is " + randomQuestionNumber);
    		}  		
    	}
    	    	
    	return questions.get(randomQuestionNumber);   
    }
    
    private void correctAnswer()
    {
    	questionsCorrect++;
    	et1.setText("Correct");
    	answerTextSet = true;
    	//setAllOptionsNotClickable();
    }
    
    public void onClick(View arg0) 
    {    	    
    	setAllOptionsClickable();
    	if (answerTextSet)
    	{   
    		answerTextSet = false;
    		
    		if (questionNumber+1 >= NO_OF_QUESTIONS)
    		{
    			showResultsPage();
    		}
    		else
    		{	    		   		
	    		et1.setText("");
	    		questionNumber++;
	    		currentQuestion = getRandomQuestion();   		
	    		displayQuestion();    
    		}
    	}
    	else
    	{	 
    		
    		
    		
    		
		    if (c1.isChecked() && currentQuestion.getOptionNumberAnswer() == 1)
		    {
		    	
		    	correctAnswer();
		    }
		    	
		    else if (c2.isChecked()  && currentQuestion.getOptionNumberAnswer() == 2)
		    {

		    	correctAnswer();
		    }
		    else if  (c3.isChecked()  && currentQuestion.getOptionNumberAnswer() == 3)
		    {

		    	correctAnswer();
		    }
		    else if  (c4.isChecked()  && currentQuestion.getOptionNumberAnswer() == 4)
		    {

		    	correctAnswer();
		    }
		    else if  (c5.isChecked()  && currentQuestion.getOptionNumberAnswer() == 5)
		    {
		    	correctAnswer();
		    }
		    else
		    {
		    	answerTextSet = true;
		    	et1.setText("");
	    	
		    	String answerText = "";
		    	for (String answerLine : currentQuestion.getLinesOfAnswer())
		    	{		    		
		    		answerText+=answerLine+"<br>";		    		
		    	}		    	
		    	et1.setText(Html.fromHtml(answerText));		    	
		    	setAllOptionsNotClickable();
		    }
		   
		    if (answerTextSet == false)
		    {
		    	if (questionNumber+1 >= NO_OF_QUESTIONS)
	    		{
	    			showResultsPage();
	    		}
	    		else
	    		{
			    	questionNumber++;
			    	currentQuestion = getRandomQuestion();		    		
			    	displayQuestion();
	    		}
		    }
    	}    
    }
    
    private void uncheckAllOptions()
    { 	    
 	    	
 	    
 	   c1.setChecked(true);
	    c2.setChecked(true);
	    c3.setChecked(true);
	    c4.setChecked(true);
	    c5.setChecked(true);   
	    
	    c1.setChecked(false);
 	    c2.setChecked(false);
 	    c3.setChecked(false);
 	    c4.setChecked(false);
 	    c5.setChecked(false);   
    }
    
    private void showAllOptions()
    {
    	c1.setVisibility(View.VISIBLE);
    	c2.setVisibility(View.VISIBLE);
    	c3.setVisibility(View.VISIBLE);
    	c4.setVisibility(View.VISIBLE);
    	c5.setVisibility(View.VISIBLE);	
    }
    
    private void loadAllOptions()
    {
    	c1 = (RadioButton) findViewById(R.id.check1);	    	
 	    c2 = (RadioButton) findViewById(R.id.check2);
 	    c3 = (RadioButton) findViewById(R.id.check3);
 	    c4 = (RadioButton) findViewById(R.id.check4);
 	    c5 = (RadioButton) findViewById(R.id.check5);	
    }
    
    private void setOptionText()
    {
    	c1.setText(currentQuestion.getOption1());
    	c2.setText(currentQuestion.getOption2());
    	c3.setText(currentQuestion.getOption3());
    	c4.setText(currentQuestion.getOption4());
    	c5.setText(currentQuestion.getOption5());    	
    }
    
    private void setAllOptionsClickable()
    {
    	c1.setClickable(true);
		c2.setClickable(true);
		c3.setClickable(true);
		c4.setClickable(true);
		c5.setClickable(true);		
    }
    
    private void setAllOptionsNotClickable()
    {    	
    	c1.setClickable(false);
		c2.setClickable(false);
		c3.setClickable(false);		
		c4.setClickable(false);
		c5.setClickable(false);	
    }
}